package com.ygy.jxc.controller;


import com.ygy.jxc.model.CkmdDepository;
import com.ygy.jxc.model.XtmdMenu;
import com.ygy.jxc.service.CkDepositoryService;
import com.ygy.jxc.util.commonPage;
import com.ygy.jxc.util.commonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "CkDepositoryController" , description = "ck-1仓库列表")
@Tag(name = "CkDepositoryController", description = "仓库管理-仓库列表")
@RequestMapping("/depository")
public class CkDepositoryController {

    @Autowired
 private CkDepositoryService depositoryService;

    @ApiOperation("添加仓库")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public commonResult create(@RequestBody  CkmdDepository depository) {
        System.out.println("添加："+depository.getId());
        int count = depositoryService.updateOrAddById(depository);
        if (count > 0) {
            return commonResult.success("成功");
        }
        return commonResult.failed();
    }

    @ApiOperation("修改仓库-归并到add")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public commonResult update(@RequestBody CkmdDepository depository) {
        int count = depositoryService.update( depository);
        if (count > 0) {
            return commonResult.success(count);
        }
        return commonResult.failed();
    }

    @ApiOperation("删除仓库")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public commonResult delete(Long id) {
        int count = depositoryService.delete(id);
        if (count > 0) {
            return commonResult.success("成功");
        }
        return commonResult.failed();
    }

    @ApiOperation("获取所有仓库")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public commonResult<List<XtmdMenu>> listAll() {
        List<XtmdMenu> depositoryList = depositoryService.list();
        return commonResult.success(depositoryList);
    }

    @ApiOperation("根据角色名称分页获取仓库列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public commonResult<commonPage<CkmdDepository>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<CkmdDepository> menuList = depositoryService.list(keyword, pageSize, pageNum);
        return commonResult.success(commonPage.restPage(menuList));
    }

    //验证使用Post请求
    @ApiOperation(value = "仓库是否存在",notes="校验仓库名称")
    @RequestMapping(value = "/checkDepositoryName",method = RequestMethod.GET)
    @ResponseBody  //没有这个注解将没有返回内容
    public commonResult checkDepositoryName(String depositoryName){
//        System.out.println("判断仓库是否存在----参数:"+depositoryName);
        boolean result = depositoryService.checkUserName(depositoryName);
        System.out.println("仓库是否存在:"+result);
        String  flag=  result ? "YES":"NO";  //存在 yes  否则为NO
        if (result){  //说明 如果rsult为true, 表示存在
            return  commonResult.success("YES");
        }else {
            return  commonResult.success("NO");  //不存在
        }
    }

        //获取仓库地图
        @ApiOperation(value = "大屏-全国仓库地图",notes = "地图")
        @RequestMapping(value = "/getDepositoryToEChart",method = RequestMethod.GET)
        @ResponseBody
    public  commonResult<List> getDepositoryToEchart(){
       List  list =depositoryService.getDepositoryToEChart();
            if (list !=null){
                return  commonResult.success(list);
            }
            return  commonResult.failed("数据为空");
        }

        @ApiOperation(value = "获取仓库数量",notes = "仓库数量")
        @RequestMapping(value = "/getLength",method = RequestMethod.GET)
        @ResponseBody
        public commonResult getLengthToDepository(){
            int count =    depositoryService.getLength();
            System.out.println("仓库数量"+count);
            if (count >0){
                return commonResult.success(count);
            }else{
                return  commonResult.failed();
            }
        }

}
